//
//  HJLoginViewController.m
//  Sneakers
//
//  Created by 胡光健 on 2017/9/28.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HJLoginViewController.h"
#import "HJRegisterViewController.h"

@interface HJLoginViewController ()

@property (weak, nonatomic) IBOutlet UIView *superView;
@property (weak, nonatomic) IBOutlet UITextField *phoneText;
@property (weak, nonatomic) IBOutlet UITextField *passwordText;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topH;
@property (nonatomic,assign) BOOL loginBool;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *centerViewH;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *phoneImageH;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *passwordImageH;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *btmViewH;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *registerBtnH;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *btnH;
@property (weak, nonatomic) IBOutlet UIButton *registerBrn;
@property (weak, nonatomic) IBOutlet UIButton *wangjiBtn;
@property (weak, nonatomic) IBOutlet UIButton *login;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *sanH;
@end

@implementation HJLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.loginBool = NO;
    self.leftNavImageName = @"Fill 1";
    [mNotificationCenter addObserver:self selector:@selector(didRegisterSuccessNotification) name:@"didRegisterSuccessNotification" object:nil];
    
    
    self.superView.layer.borderWidth = 1.0;
    self.superView.layer.borderColor = HJColor(192, 204, 218).CGColor;
    self.phoneText.font = HJFontSize(mScreenWidth/414 *19);
    self.passwordText.font = HJFontSize(mScreenWidth/414 *19);
    self.topH.constant = mScreenWidth/414*60;
    self.centerViewH.constant = mScreenWidth/414*120;
    self.phoneImageH.constant = mScreenWidth/414*20;
    self.passwordImageH.constant = mScreenWidth/414*20;
    self.btnH.constant=mScreenWidth/414*50;
    self.btmViewH.constant = mScreenWidth/414*150;
    self.registerBtnH.constant = mScreenWidth/414*20;
    self.sanH.constant = mScreenWidth/414*20;
    self.registerBrn.titleLabel.font = HJFontSize(mScreenWidth/414 * 15);
    self.wangjiBtn.titleLabel.font = HJFontSize(mScreenWidth/414 * 15);
    self.login.titleLabel.font = HJFontSize(mScreenWidth/414 * 23);
}



- (IBAction)eyeBtn:(UIButton *)sender {
    if (sender.selected) {
        self.passwordText.secureTextEntry = YES;
    }else {
        self.passwordText.secureTextEntry = NO;
    }
    sender.selected = !sender.selected;
}

- (IBAction)loginBtn:(UIButton *)sender {
     [self.view endEditing:YES];
    [self HTTPLogin];
    
}
- (void)didRegisterSuccessNotification {
    NSString *password = [mUserDefaults objectForKey:@"password"];
    if (password.length) {
        self.passwordText.text = password;
    }
    NSString *username = [mUserDefaults objectForKey:@"phone"];
    if (username.length) {
        self.phoneText.text = username;
    }
    [self HTTPLogin];
    
}

-(void)HTTPLogin {
    NSString *userName;
    NSString *password;
    NSString *client_id = [mUserDefaults objectForKey:@"client_id"];
    if (!userName) userName = self.phoneText.text;
    if (!password) password = self.passwordText.text;
    if (userName && userName.length && password && password.length) {
        NSMutableDictionary *param = [NSMutableDictionary dictionary];
        param[@"mobile"] = userName;
        param[@"password"] = password;
        param[@"client_id"] = client_id;
        [self showTextHUDWithPleaseWait];
        [TZHttpTool post:Login params:param success:^(NSDictionary *result) {
            [HJUserManager setUserModelWithDict:result[@"data"]];
            [mUserDefaults setObject:userName forKey:@"phone"];
            [HJEaseManager loginChatClientWithCompletion:^(BOOL success) {
                if (success) {
                    [self didLoginSuccessWithResult:result];
                } else {
                    [self hideTextHud];
                    [self didLoginSuccessWithResult:result];
                }
            }];
        } failure:^(NSString *msg) {
            [self showErrorHUDWithError:msg];
        }];
    }else{
        [self showHint:@"请输入账号和密码"];
    }
}
- (void)didLoginSuccessWithResult:(NSDictionary *)result {
    [self showSuccessHUDWithStr:@"登录成功"];
    [mUserDefaults setObject:result[@"authToken"] forKey:@"authToken"];
    [mUserDefaults setObject:[NSString stringWithFormat:@"%@码",result[@"data"][@"often_size"]] forKey:@"often_size"];
    [mUserDefaults setObject:@"1" forKey:@"isLogin"];
    [mUserDefaults synchronize];
//    [mNotificationCenter postNotificationName:@"didLoginSuccess" object:nil];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.75 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self dismissViewControllerAnimated:YES completion:nil];
    });
    // 同步推送消息
    [self hideTextHud];
}

- (IBAction)forgotPasswordBtn:(UIButton *)sender {
    [self pushFprgotPasswordWithRegisterWithTitle:@"忘记密码"];
}
// 三方登录
- (IBAction)threeLoginBtn:(UIButton *)sender {
    MJWeakSelf;
    if (!self.loginBool) {
        if (sender.tag == 2) {
            if (![WXApi isWXAppInstalled]) {
                [self showSuccessHUDWithStr:@"您尚未安装微信"];
            } else {
                [[HJShareManager manager] shareLogin:SSDKPlatformTypeWechat tag:@"1" success:^(NSDictionary *resul) {
                    [weakSelf didLoginSuccessWithResult:resul];
                }];
            }
        } else if (sender.tag == 1) {
            if (![QQApiInterface isQQInstalled]) {
                [self showSuccessHUDWithStr:@"您尚未安装QQ"];
            } else {
                [[HJShareManager manager] shareLogin:SSDKPlatformTypeQQ tag:@"2" success:^(NSDictionary *resul) {
                    [weakSelf didLoginSuccessWithResult:resul];
                }];
            }
//            if (![WeiboSDK isWeiboAppInstalled]) {
//                [self showSuccessHUDWithStr:@"您尚未安装微博"];
//            } else {
//                [[HJShareManager manager] shareLogin:SSDKPlatformTypeSinaWeibo tag:@"3" success:^(NSDictionary *resul) {
//                    [weakSelf didLoginSuccessWithResult:resul];
//                }];
//            }
        }
        self.loginBool = YES;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.loginBool = NO;
        });
    }
}

- (IBAction)registerBtn:(UIButton *)sender {
    [self pushFprgotPasswordWithRegisterWithTitle:@"注册"];
}

-(void)pushFprgotPasswordWithRegisterWithTitle:(NSString *)navTitle {
    HJRegisterViewController * registerVc = [[HJRegisterViewController alloc]init];
    registerVc.navTitle = navTitle;
    [self.navigationController pushViewController:registerVc animated:YES];
}
-(void)didClickLeftNavAction {
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
